//
//  HSLeftViewController.swift
//  HomeSchoolDemo
//
//  Created by Mac on 16/9/27.
//  Copyright © 2016年 chendong. All rights reserved.
//

import UIKit
import SnapKit

class HSLeftViewController: UIViewController {
    
    ///头像
    @IBOutlet weak var leftIconImageView: UIImageView!
    ///用户名称
    @IBOutlet weak var leftNameLabel: UILabel!
    ///账号
    @IBOutlet weak var leftAccountLabel: UILabel!
    ///班级名称
    @IBOutlet weak var leftClassLabel: UILabel!
    ///学校名称
    @IBOutlet weak var leftSchoolLabel: UILabel!
    ///表现分
    @IBOutlet weak var leftScoreLabel: UILabel!
    ///分数分级视图
    @IBOutlet weak var leftScoreBgView: UIView!
    ///头像宽度
    @IBOutlet weak var leftIconConstraint: NSLayoutConstraint!
    
    @IBOutlet var leftBtnArray: [UIButton]!
    
    
    @IBAction func leftBtnCliced(_ sender: UIButton) {
        switch sender.currentTitle! {
        case "课程表":
            print("点击了课程表")
            present(HSClassTableController(), animated: true, completion: nil)
        case "关于我们":
            print("点击了关于我们")
            let aboutVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HSAboutWeController")
            present(aboutVC, animated: true, completion: nil)
        case "意见反馈":
            print("点击了意见反馈")
            let feedbackVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HSFeedbackViewController")
            present(feedbackVC, animated: true, completion: nil)
            
        default:
            //弹窗确认是否退出
            let alertVC = UIAlertController(title: "确定要推出吗", message: nil, preferredStyle: .alert)
            //添加确定按钮
            alertVC.addAction(UIAlertAction(title: "确定", style: .default, handler: { (_) in
                //获得登录界面
                let loginVc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HSLoginController")
                UIApplication.shared.keyWindow?.rootViewController = loginVc
                //修改登录状态
                UserDefaults.standard.set(false, forKey: isLogin)

            }))
            alertVC.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil))
            present(alertVC, animated: true, completion: nil)
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        
    }
    //设置UI
    func setupUI() {
        //设置背景
        view.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        //调整头像大小
        leftIconConstraint.constant = screenW <= 320 ? 60 :80
        //设置头像圆角
        leftIconImageView.layer.cornerRadius = leftIconConstraint.constant / 2
        leftIconImageView.layer.masksToBounds = true
        //设置边框
        leftIconImageView.layer.borderWidth = 2
        leftIconImageView.layer.borderColor = UIColor.white.cgColor
        //设置左边按钮的高亮背景
        for btn in leftBtnArray{
            let image = UIImage.cd_image(color: #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1), size: btn.bounds.size)
            btn.setBackgroundImage(image, for: .highlighted)
        }
        //leftScoreBgView.backgroundColor = UIColor.colorWithHex(0x009ee7)
        //添加等级视图
        for i in 1...5{
            //初始化图片视图
            let imageV = UIImageView(image: UIImage(named: "jujube\(i)_dark"))
            imageV.tag = 99 + i
            leftScoreBgView.addSubview(imageV)
        }
        
        let userInfo = NSDictionary(contentsOfFile: saveUserfoPath)?["data"] as? NSDictionary
        //头像
        if let iconStr = userInfo?["studentphoto"] as? String {
        let iconData = Data(base64Encoded: iconStr, options: Data.Base64DecodingOptions.ignoreUnknownCharacters)
            leftIconImageView.image = UIImage(data:iconData!)
        }
        //姓名
        leftNameLabel.text = (userInfo?["studentName"] as? String ?? "")+"    家长"
        leftAccountLabel.text = userInfo?["studentNum"] as? String
        leftClassLabel.text = userInfo?["classname"] as? String
        leftSchoolLabel.text = userInfo?["schoolname"] as? String
        leftScoreLabel.text = userInfo?["studentScore"] as? String
        
        /*
         data =     {
         address = "*";
         classid = 1071394;
         classname = "\U4e94(1)\U73ed";
         grade = "\U4e94\U5e74\U7ea7";
         id = 5;
         key = 5;
         qq = "*";
         schoolname = "\U9f99\U817e\U4e91\U6559\U80b2\U79d1\U6280";
         sex = "\U5973";
         studentName = "\U66f9\U9756\U96ef";
         studentNum = 5105;
         studentScore = 1;
        */
    }
        
    
    //视图的生命周期方法
    override func viewDidAppear(_ animated: Bool) {
        // 添加视图
        for iv in leftScoreBgView.subviews {
            // 根据背景图的宽度获得图片左边的距离
            let margin = leftScoreBgView.frame.width / 5
            // 计算x中心值
            let x = margin * CGFloat(iv.tag - 100) + iv.bounds.width/2
            // 设置约束
            iv.snp.makeConstraints({ (make) in
                make.centerX.equalTo(leftScoreBgView.snp.left).offset(x)
                make.bottom.equalTo(leftScoreBgView.snp.bottom).offset(-5)
            })
        }
        
        
    }
    
    
    
    
    
    
}
